/**
 * 
 */
package org.springside.examples.miniweb.web.account;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.examples.miniweb.entity.Company;
import org.springside.examples.miniweb.service.account.CompanyManager;

/**
 * @author Bob
 * 
 */
@Controller
@RequestMapping(value = "/company")
public class CompanyController {

	private CompanyManager companyManager;

	@RequestMapping(value = { "list", "" })
	public String list(Model model) {
		List<Company> companys = companyManager.getAllCompany();
		model.addAttribute("companys", companys);
		return "company/companyList";
	}

	@RequestMapping(value = "create")
	public String createForm(Model model) {
		model.addAttribute("company", new Company());
		return "company/companyForm";
	}

	@RequestMapping(value = "save")
	public String save(Company company, RedirectAttributes redirectAttributes) {
		companyManager.saveCompany(company);
		redirectAttributes.addFlashAttribute("message",
				"创建机构" + company.getName() + "成功");
		return "redirect:/company/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		companyManager.deleteCompany(id);
		redirectAttributes.addFlashAttribute("message", "删除机构成功");
		return "redirect:/company/";
	}

	/**
	 * @param companyManager
	 *            the companyManager to set
	 */
	@Autowired
	public void setCompanyManager(CompanyManager companyManager) {
		this.companyManager = companyManager;
	}

}
